// **********************************************************************
//                            Project Nitro
//                        ---------------------
//
//  File        : GameEngine.cpp
//  Description : Game Engine
//  Author      : Guillaume Delhumeau <guillaume.delhumeau@gmail.com>
//  License     : GNU General Public License 3
//
// **********************************************************************

#include "GameEngine.h"
#include "Preferences.h"
#include "CarResources.h"
#include "Car.h"
#include "Level.h"

/************************************************************************/
/* Constructor                                                          */
/************************************************************************/
GameEngine::GameEngine():
_running(false),
_world_display(_screen)
{
	
}

/************************************************************************/
/* Init game engine                                                     */
/************************************************************************/
void GameEngine::init()
{
	switch(g_preferences.render_target)
	{
	case OPENGL_1:
		target_GL1.set_current();
		break;

	case GDI:
		target_GDI.set_current();
		break;

	case OPENGL_2:
		target_GL2.set_current();
		break;	

	case DIRECTX_9:
		target_GDI.set_current();
		break;

	case DIRECTX_10:
		target_GDI.set_current();
		break;
	}
	_screen.manage_window(*this);
}

/************************************************************************/
/* Run game engine                                                      */
/************************************************************************/
void GameEngine::run()
{
	CarResources car_resources;
	car_resources.load_resources(_screen, "resources/cars/YellowF1.cres");
	Car car;
	car.set_resources(car_resources);

	// Load level
	Level level;
	level.load_level_file("resources/levels/kazu.lvl", _screen);

	_world_display.set_level(&level);
	_world_display.set_main_car(&car);

	_running = true;
	while (_running)
	{	
		_world_display.draw();
		car.events(_screen);		
		car.update(_screen);
	}
}

/************************************************************************/
/* Stop the main loop                                                   */
/************************************************************************/
void GameEngine::stop()
{
	_running = false;
}

/************************************************************************/
/* Terminate game engine                                                */
/************************************************************************/
void GameEngine::term()
{

}